package com.songshuai.pangu.api.controller;


import com.alibaba.fastjson.JSONObject;
import com.songshuai.webapi.baseapi.utils.EncryptUtil;
import com.songshuai.webapi.common.httpclient.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class BaseController {

    @Value("${pangu.api.secret.key}")
    public String pangguSecretKey;

    /**
     * 调用盘古系统提供的接口
     * @param body
     * @param url
     * @return
     */
    protected String callPanguApi(JSONObject body,String url){
        Map<String,String> param=new ConcurrentHashMap<>();
        String timestamp = String.valueOf(new Date().getTime());
        param.put("timestamp",timestamp);
       // param.put("token", EncryptUtil.md5(body.toJSONString()+"pangu"));
        param.put("token", EncryptUtil.md5(pangguSecretKey+timestamp));
        log.info("body内容：{}", body.toJSONString());
        String s = HttpClientUtil.doPostHttps(url, body.toJSONString(), param, 5000);
        log.info("调用远程接口"+"POST HTTPS &&&"+"url="+url+"&&&& header="+param.toString()+" &&&&body="+body.toJSONString()+" &&&&返回结果"+s);
        return s;
    }

    protected String getCallPanguApi(Map param, String url){
        Map<String,String> header=new ConcurrentHashMap<>();
        String timestamp = String.valueOf(new Date().getTime());
        header.put("timestamp",timestamp);
        header.put("token", EncryptUtil.md5(pangguSecretKey + timestamp));
        String s = HttpClientUtil.sendGetHttps(url, param, header);
        log.info("调用远程接口"+"GET HTTPS &&&"+"url="+url+"&&&& header="+header.toString()+" &&&&body="+param.toString()+" &&&返回结果"+s);
        return s;
    }

    protected String callPanguApiFile(MultipartFile file, String url){
        Map<String,String> header=new ConcurrentHashMap<>();
        String timestamp = String.valueOf(new Date().getTime());
        header.put("timestamp",timestamp);
        header.put("token", EncryptUtil.md5(pangguSecretKey + timestamp));
        String s = HttpClientUtil.doPostForm(url, header, file, "file", 5000);
        return s;
    }

    protected  String callPhpSopApi(JSONObject body, String url){
        Map<String,String> header = new ConcurrentHashMap<>();
        String param = body.toJSONString();
        log.info("请求调用第三方接口前参数信息：{}", param);
        String result = HttpClientUtil.doPostJson(url, param, header, 5000);
        log.info("请求第三方接口返回结果result：{}", result);
        return result;
    }
}
